#!/usr/bin/python3
# -*- coding: utf-8 -*-
from PyQt5.QtDBus import *


dbus_service = "org.freedesktop.Notifications"
dbus_path = "/org/freedesktop/Notifications"
dbus_interface = "org.freedesktop.Notifications"
send_notify = "NotifyPy"
close_notify = "CloseNotification"
m_notifyId = 0

# 通知函数：默认动作跳转（点击消息体跳转）和按钮跳转 demo
def DefaultAndButtonAction() :
    appName = "App Name"  # 应用名字
    replaceId = 0  # 替换通知
    appIcon = "APP Icon"   # 应用Icon,使用主题图标即可
    notifySummary = "Notify Summary ......"  # 通知主题
    notifyBody = "Notify Body ......"   # 通知正文
    action = ["kylin-recorder", "default", "ukui-control-center", "控制面板", "kylin-music", "麒麟音乐"]  # 动作跳转（没有跳转可设置为空）
    hints = {}
    timeout = 0  # 消息显示时间  -1：默认显示时间（由通知中心决定，一般为3秒） 0：消息常驻  其他：显示相应时长(由应用决定，单位ms)
    args = [appName, replaceId, appIcon, notifySummary, notifyBody, action, hints, timeout]
    try:
        dbus = QDBusConnection.sessionBus()
    except:
        return
    try:
        iface = QDBusInterface(dbus_service, dbus_path, dbus_interface, dbus)
        res = iface.callWithArgumentList(QDBus.AutoDetect, send_notify, args)
        global m_notifyId
        m_notifyId = int(res.arguments()[0])
        print(m_notifyId)
    except Exception as e:
        pass

# 链接跳转：例如截图应用，点击弹窗后会使用默认应用打开图片进行预览
def UrlAction() :
    appName = "App Name"  # 应用名字
    replaceId = 0  # 替换通知
    appIcon = "APP Icon"  # 应用Icon,使用主题图标即可
    notifySummary = "Notify Summary ......"  # 通知主题
    # 链接跳转正文格式为： <a href="跳转链接"> 要显示的文本内容 </a>
    bodyUrl = "/usr/share/icons/ukui-icon-theme-fashion/scalable/apps/mate-about.svg"
    bodyText = "显示的正文内容：mate-about.svg"
    notifyBody = "<a href=\"" + bodyUrl + "\">" + bodyText + "</a>"
    action = []  # 动作跳转（没有跳转可设置为空）
    hints = {}
    timeout = -1  # 消息显示时间  -1：默认显示时间（由通知中心决定，一般为3秒） 0：消息常驻  其他：显示相应时长(由应用决定，单位ms)
    args = [appName, replaceId, appIcon, notifySummary, notifyBody, action, hints, timeout]
    try:
        dbus = QDBusConnection.sessionBus()
    except:
        return
    try:
        iface = QDBusInterface(dbus_service, dbus_path, dbus_interface, dbus)
        res = iface.callWithArgumentList(QDBus.AutoDetect, send_notify, args)
        global m_notifyId
        m_notifyId = int(res.arguments()[0])
    except Exception as e:
        pass

# 关闭通知: 通知中心暂时不支持
# def CloseNotify() :
#     cliseId = m_notifyId
#     print("----")
#     print(cliseId)
#     args = [cliseId]
#     print(args)
#     try:
#         dbus = QDBusConnection.sessionBus()
#     except:
#         return
#     try:
#         iface = QDBusInterface(dbus_service, dbus_path, dbus_interface, dbus)
#         res = iface.callWithArgumentList(QDBus.AutoDetect, close_notify, args)
#     except Exception as e:
#         pass



# 动作跳转
DefaultAndButtonAction()
# 链接跳转
# UrlAction()